package com.bawei.module_live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.media3.ui.PlayerView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_live.R
import com.bawei.module_live.version1.VideoEntityItem
import com.bumptech.glide.Glide

class  VideoPagerAdapter : RecyclerView.Adapter<VideoPagerAdapter.VideoViewHolder>() {
    
    private var videoList: List<VideoEntityItem> = emptyList()
    private var onVideoClickListener: ((VideoEntityItem, Int) -> Unit)? = null
    private var onVideoPlayListener: ((VideoEntityItem, Int, PlayerView) -> Unit)? = null
    private var onVideoPauseListener: ((VideoEntityItem, Int) -> Unit)? = null
    
    fun updateData(newList: List<VideoEntityItem>) {
        // 只保留有有效视频源的 item（只要求 http/https 开头）
        videoList = newList.filter {
            !it.videopath.isNullOrEmpty() &&
            it.videopath != "null" &&
            (it.videopath.startsWith("http://") || it.videopath.startsWith("https://"))
        }
        notifyDataSetChanged()
    }
    
    fun setOnVideoClickListener(listener: (VideoEntityItem, Int) -> Unit) {
        onVideoClickListener = listener
    }
    
    fun setOnVideoPlayListener(listener: (VideoEntityItem, Int, PlayerView) -> Unit) {
        onVideoPlayListener = listener
    }
    
    fun setOnVideoPauseListener(listener: (VideoEntityItem, Int) -> Unit) {
        onVideoPauseListener = listener
    }
    
    fun getVideoAtPosition(position: Int): VideoEntityItem? {
        return videoList.getOrNull(position)
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_video_player, parent, false)
        return VideoViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
        val video = videoList.getOrNull(position)
        if (video != null) {
            holder.bind(video, position)
        }
    }
    
    override fun getItemCount(): Int = videoList.size
    
    inner class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        
        private val playerView: PlayerView = itemView.findViewById(R.id.player_view)
        private val ivVideoCover: ImageView = itemView.findViewById(R.id.iv_video_cover)
        private val ivAvatar: ImageView = itemView.findViewById(R.id.iv_avatar)
        private val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        private val tvDescription: TextView = itemView.findViewById(R.id.tv_description)
        private val tvUserName: TextView = itemView.findViewById(R.id.tv_user_name)
        private val tvPlayCount: TextView = itemView.findViewById(R.id.tv_play_count)
        private val tvCommentCount: TextView = itemView.findViewById(R.id.tv_comment_count)
        private val btnLike: ImageView = itemView.findViewById(R.id.btn_like)
        private val btnComment: ImageView = itemView.findViewById(R.id.btn_comment)
        private val btnShare: ImageView = itemView.findViewById(R.id.btn_share)
        
        fun bind(video: VideoEntityItem, position: Int) {
            // 设置视频封面
            if (!video.image_url.isNullOrEmpty()) {
                ivVideoCover.visibility = View.VISIBLE
                Glide.with(itemView.context)
                    .load(video.image_url)
                    .into(ivVideoCover)
            } else {
                // 没有封面时直接隐藏
                ivVideoCover.visibility = View.GONE
            }
            
            // 设置用户头像
            if (!video.avatar_url.isNullOrEmpty()) {
                Glide.with(itemView.context)
                    .load(video.avatar_url)
                    .circleCrop()
                    .into(ivAvatar)
            } else {
                ivAvatar.setImageDrawable(null)
            }
            
            // 设置文本内容
            tvTitle.text = video.title.takeIf { !it.isNullOrEmpty() } ?: "无标题"
            tvDescription.text = video.description.takeIf { !it.isNullOrEmpty() } ?: "无描述"
            tvUserName.text = video.name.takeIf { !it.isNullOrEmpty() } ?: "未知用户"
            tvPlayCount.text = "播放量: ${video.playnum}"
            tvCommentCount.text = "评论: ${video.commentnum}"
            
            // 设置点击事件
            itemView.setOnClickListener {
                onVideoClickListener?.invoke(video, position)
            }
            
            // 设置按钮点击事件
            btnLike.setOnClickListener {
                // 点赞功能
                Toast.makeText(itemView.context, "点赞", Toast.LENGTH_SHORT).show()
            }
            
            btnComment.setOnClickListener {
                // 评论功能
                Toast.makeText(itemView.context, "评论", Toast.LENGTH_SHORT).show()
            }
            
            btnShare.setOnClickListener {
                // 分享功能
                Toast.makeText(itemView.context, "分享", Toast.LENGTH_SHORT).show()
            }
        }
        
        fun getPlayerView(): PlayerView {
            return playerView
        }
        
        fun getVideoCover(): ImageView {
            return ivVideoCover
        }
    }
} 